package cn.com.ghy.android.bean;

import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import cn.com.ghy.android.util.XMLStatic;

import android.util.Xml;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:订单详细信息解析类
 * @author wuz  @date 2011-5-17 10:15:00
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class OrderDetailBeanParser {
	public static List<OrderDetailBean> parseXML(InputStream in) throws Exception {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, "UTF-8");
        int type = parser.getEventType(); 
        
        OrderDetailBean orderDetail = null;
        UnitInfoBean unitInfo = null;
        List<OrderDetailBean> list = null;
        
        while (type != XmlPullParser.END_DOCUMENT) { 
            switch (type) {   
            case XmlPullParser.START_DOCUMENT:   
            	list = new ArrayList<OrderDetailBean>();   
                break;   
   
            case XmlPullParser.START_TAG:
                if ("Table".equals(parser.getName())) {
                	orderDetail = new OrderDetailBean();
                	unitInfo = new UnitInfoBean();
                } else if (null != orderDetail) {
                    if ("BISMT".equals(parser.getName())) {   
                    	orderDetail.setBismt(parser.nextText());   
                    } else if("MATNR".equals(parser.getName())) {
                    	orderDetail.setMatnr(parser.nextText());
                    } else if("MAKTX".equals(parser.getName())) {
                    	orderDetail.setMaktx(parser.nextText());
                    } else if("ORDERPRICE".equals(parser.getName())) {
                    	orderDetail.setOrderprice(parser.nextText());
                    } else if("NUM".equals(parser.getName())) {
                    	orderDetail.setNum(parser.nextText());
                    } else if("TOTALPRICE".equals(parser.getName())) {
                    	orderDetail.setTotalprice(parser.nextText());
                    } else if("UNIT".equals(parser.getName())) {
                    	unitInfo.setUnit(parser.nextText());
                    } else if("UNITNAME".equals(parser.getName())) {
                    	unitInfo.setUnitname(parser.nextText());
                    }
                }   
                break;   
   
            case XmlPullParser.END_TAG:   
                if ("Table".equals(parser.getName())) {
                	if (null != unitInfo) {
                		orderDetail.setUnitInfo(unitInfo);
                		unitInfo = null;
                	}
                	list.add(orderDetail);   
                	orderDetail = null;   
                }
                break;
            }
            type = parser.next();
        }   
        in.close();   
        return list;  
    }
	
	public static String createXMLOrderhead(CustomerInfoBean customerInfo, String orderno, 
			String xqriqi, LoginInfoBean loginInfo, Writer writer) throws Exception {
        XmlSerializer xmlSerializer = Xml.newSerializer();
        
        xmlSerializer.setOutput(writer);
        xmlSerializer.startDocument("UTF-8", true);
        xmlSerializer.startTag("", XMLStatic.CONTENT);
        xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, "1");
        xmlSerializer.startTag("", XMLStatic.RECORDS);
        
        if(null != customerInfo.getOrderReportInfoBean()) {
        	xmlSerializer.startTag("", XMLStatic.ORDERID);
            xmlSerializer.text(customerInfo.getOrderReportInfoBean().getOrderid());
    		xmlSerializer.endTag("", XMLStatic.ORDERID);
        }
        
        xmlSerializer.startTag("", XMLStatic.CUSTNAME);
        xmlSerializer.text(customerInfo.getName_org1());
		xmlSerializer.endTag("", XMLStatic.CUSTNAME);
		xmlSerializer.startTag("", XMLStatic.ORDERNO);
        xmlSerializer.text(orderno);
		xmlSerializer.endTag("", XMLStatic.ORDERNO);
		xmlSerializer.startTag("", XMLStatic.PARTNER);
        xmlSerializer.text(customerInfo.getPartner());
		xmlSerializer.endTag("", XMLStatic.PARTNER);
		xmlSerializer.startTag("", XMLStatic.SAOF);
        xmlSerializer.text(loginInfo.getSaof());
		xmlSerializer.endTag("", XMLStatic.SAOF);
		xmlSerializer.startTag("", XMLStatic.FXSID);
        xmlSerializer.text(null == customerInfo.getFxsid() ? "" : customerInfo.getFxsid());
		xmlSerializer.endTag("", XMLStatic.FXSID);
		xmlSerializer.startTag("", XMLStatic.FXSNAME);
        xmlSerializer.text(null == customerInfo.getFxsname() ? "" : customerInfo.getFxsname());
		xmlSerializer.endTag("", XMLStatic.FXSNAME);
		xmlSerializer.startTag("", XMLStatic.LXR);
        xmlSerializer.text(customerInfo.getLxr());
		xmlSerializer.endTag("", XMLStatic.LXR);
		xmlSerializer.startTag("", XMLStatic.TEL);
        xmlSerializer.text(customerInfo.getLxdh());
		xmlSerializer.endTag("", XMLStatic.TEL);
		xmlSerializer.startTag("", XMLStatic.ADDRESS);
        xmlSerializer.text(customerInfo.getStreet());
		xmlSerializer.endTag("", XMLStatic.ADDRESS);
		xmlSerializer.startTag("", XMLStatic.XQRIQI);
        xmlSerializer.text(xqriqi);
		xmlSerializer.endTag("", XMLStatic.XQRIQI);
		xmlSerializer.startTag("", XMLStatic.JXS);
        xmlSerializer.text(null == customerInfo.getJxsid() ? "" : customerInfo.getJxsid());
		xmlSerializer.endTag("", XMLStatic.JXS);
		xmlSerializer.startTag("", XMLStatic.JXSNAME);
        xmlSerializer.text(null == customerInfo.getJxsname() ? "" : customerInfo.getJxsname());
		xmlSerializer.endTag("", XMLStatic.JXSNAME);
		xmlSerializer.startTag("", XMLStatic.PERNR);
        xmlSerializer.text(loginInfo.getPernr());
		xmlSerializer.endTag("", XMLStatic.PERNR);
		xmlSerializer.startTag("", XMLStatic.FAX);
        xmlSerializer.text(null == customerInfo.getFax() ? "" : customerInfo.getFax());
		xmlSerializer.endTag("", XMLStatic.FAX);
		xmlSerializer.startTag("", XMLStatic.TOSEND);
        xmlSerializer.text(null == customerInfo.getTosend() ? "N" : customerInfo.getTosend());
		xmlSerializer.endTag("", XMLStatic.TOSEND);
		
		xmlSerializer.startTag("", XMLStatic.ISCAR);
        xmlSerializer.text(null == customerInfo.getIsCar() ? "" : customerInfo.getIsCar());
		xmlSerializer.endTag("", XMLStatic.ISCAR);
		
		xmlSerializer.endTag("", XMLStatic.RECORDS);
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}
	
	public static String createXMLOrderdetail (List<OrderDetailBean> list,
			Writer writer) throws Exception {
        XmlSerializer xmlSerializer = Xml.newSerializer();
        
        xmlSerializer.setOutput(writer);
        xmlSerializer.startDocument("UTF-8", true);
        xmlSerializer.startTag("", XMLStatic.CONTENT);
        xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, list.size() + "");
        
        for(int i = 0; i < list.size(); i++) {
        	xmlSerializer.startTag("", XMLStatic.RECORDS);
        	xmlSerializer.startTag("", XMLStatic.TAG);
            xmlSerializer.text((i + 1) + "");
    		xmlSerializer.endTag("", XMLStatic.TAG);
    		xmlSerializer.startTag("", XMLStatic.BISMT);
            xmlSerializer.text(list.get(i).getBismt());
    		xmlSerializer.endTag("", XMLStatic.BISMT);
    		xmlSerializer.startTag("", XMLStatic.MAKTX);
            xmlSerializer.text(list.get(i).getMaktx());
    		xmlSerializer.endTag("", XMLStatic.MAKTX);
    		xmlSerializer.startTag("", XMLStatic.MATNR);
            xmlSerializer.text(list.get(i).getMatnr());
    		xmlSerializer.endTag("", XMLStatic.MATNR);
    		xmlSerializer.startTag("", XMLStatic.SHOP_CODE);
            xmlSerializer.text(null == list.get(i).getShop_code() ? "" : list.get(i).getShop_code());
    		xmlSerializer.endTag("", XMLStatic.SHOP_CODE);
    		xmlSerializer.startTag("", XMLStatic.UNIT);
            xmlSerializer.text(list.get(i).getUnitInfo().getUnit());
    		xmlSerializer.endTag("", XMLStatic.UNIT);
    		xmlSerializer.startTag("", XMLStatic.NUM);
            xmlSerializer.text(list.get(i).getNum());
    		xmlSerializer.endTag("", XMLStatic.NUM);
    		xmlSerializer.startTag("", XMLStatic.ORDERPRICE);
            xmlSerializer.text(list.get(i).getOrderprice());
    		xmlSerializer.endTag("", XMLStatic.ORDERPRICE);
    		xmlSerializer.startTag("", XMLStatic.REMARK);
            xmlSerializer.text(null == list.get(i).getRemark() ? "" : list.get(i).getRemark());
    		xmlSerializer.endTag("", XMLStatic.REMARK);
    		xmlSerializer.endTag("", XMLStatic.RECORDS);
        }
        
        if(0 == list.size()) {
        	xmlSerializer.startTag("", XMLStatic.RECORDS);
        	xmlSerializer.endTag("", XMLStatic.RECORDS);
        }
        
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}
}
